package com.yrmjhtdjxh.punch.conf;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.boot.SpringBootVersion;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @author GO FOR IT
 */
@EnableOpenApi
@Configuration
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {

//    private final SwaggerProperties swaggerProperties;
//
//    public SwaggerConfig(SwaggerProperties swaggerProperties) {
//        this.swaggerProperties = swaggerProperties;
//    }


    @Bean
    public Docket createRestApi() {
        Set<String> set = new HashSet<>();
        set.add("https");
        set.add("http");
        return new Docket(DocumentationType.OAS_30)
                .pathMapping("/")
                //定义是否开启swagger，false为关闭，可以通过变量控制
//                .enable(swaggerProperties.getEnable())
                .enable(true)
                //将api的元信息设置为包含在json ResourceListing响应中。
                .apiInfo(apiInfo())
                //接⼝调试地址
//                .host(swaggerProperties.getTryHost())
                //选择哪些接⼝作为swagger的doc发布
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.best.user"))//将那些包作为接⼝⽂档来显示
                .apis(RequestHandlerSelectors.basePackage("com.yrmjhtdjxh.punch.web"))
                .paths(PathSelectors.any())
                .build()
                // ⽀持的通讯协议集合
                .protocols(set)
                // 授权信息设置，必要的header token等认证信息
                .securitySchemes(securitySchemes())
                // 授权信息全局应⽤
                .securityContexts(securityContexts());
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
//                .title(swaggerProperties.getApplicationName() + "Restful APIs")
//                .description(swaggerProperties.getApplicationDescription())
//                .termsOfServiceUrl("http://localhost:8081/")
//                .version("Application Version: " + swaggerProperties.getApplicationVersion() +
//                        ", Spring Boot Version: " + SpringBootVersion.getVersion())
//                .build();
                .title("打卡系统" + "Restful APIs")
                .description("")
                .termsOfServiceUrl("http://localhost:8090/")
                .version("Application Version: " + "2.1" +
                        ", Spring Boot Version: " + SpringBootVersion.getVersion())
                .build();
    }
    /**
     * 设置授权信息
     */
    private List<SecurityScheme> securitySchemes() {
        ApiKey apiKey = new ApiKey("Authorization", "token",
                io.swagger.v3.oas.models.security.SecurityScheme.In.HEADER.toString());
        return Collections.singletonList(apiKey);
    }
    /**
     * 授权信息全局应⽤
     */
    private List<SecurityContext> securityContexts() {
        return Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(Collections.singletonList(new SecurityReference("Authorization", new
                                AuthorizationScope[]{new AuthorizationScope("global", "")})))
                        .build()
        );
    }
    @SafeVarargs
    private final <T> Set<T> newHashSet(T... ts) {
        if (ts.length > 0) {
            return new LinkedHashSet<>(Arrays.asList(ts));
        }
        return null;
    }
    /**
     * 通⽤拦截器排除swagger设置，所有拦截器都会⾃动加swagger相关的资源排除信息
     */
    @SuppressWarnings("unchecked")
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        try {
            Field registrationsField = FieldUtils.getField(InterceptorRegistry.class, "registrations",true);
            List<InterceptorRegistration> registrations = (List<InterceptorRegistration>)
                    ReflectionUtils.getField(registrationsField, registry);
            if (registrations != null) {
                for (InterceptorRegistration interceptorRegistration : registrations) {
                    interceptorRegistration
                            .excludePathPatterns("/swagger**/**")
                            .excludePathPatterns("/webjars/**")
                            .excludePathPatterns("/v3/**")
                            .excludePathPatterns("/doc.html");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}